// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import SpriteFrame = cc.SpriteFrame;
import {HeadShopData} from "../web/model/HeadModel";
import {GlobalObject} from "../game/GameVar";
import Confirm from "./confirm";
import {apiUtils} from "../web/api/ApiUtils";
import {model} from "../web/model/Model";
import {SocketUtils} from "../socket/SocketUtils";
import {utils} from "../utils/utils";
import Menu from "../menu/menu";
import {StatEvent, Statistic} from "../../statistic/statistic";

const {ccclass, property} = cc._decorator;

export enum ShopState {
  USED = 2,
  NOT_USED = 1,
  NOT_BUY = 3,
  GET = 4
}

@ccclass
export default class HeadShopItem extends cc.Component {

  private icon: cc.Sprite = null;
  @property(cc.Label)
  private txtPrice: cc.Label = null;
  @property(cc.Button)
  private btnBuy: cc.Button = null;
  @property(cc.Button)
  private btnUse: cc.Button = null;
  @property(cc.Button)
  private btnUsed: cc.Button = null;
  @property(cc.Button)
  private btnGet: cc.Button = null;
  @property(cc.Node)
  private sellIcon1: cc.Node = null;
  @property(cc.Node)
  private sellIcon2: cc.Node = null;

  private _data: HeadShopData = {}

  onLoad() {
    this.icon = this.node.getChildByName('icon').getComponent(cc.Sprite);
    this.btnUsed.interactable = false;
    this.icon.node.active = false;
    this.btnUsed.node.scale = 0.6;
  }

  start() {
    cc.loader.loadRes(`icon/${this._data.iconid}`, (err, res) => {
      this.icon.spriteFrame = new SpriteFrame(res);
      this.icon.node.active = true;
    });
  }

  private updateState(state: ShopState) {
    switch (state) {
      case ShopState.NOT_BUY:
        this.btnBuy.node.active = true;
        this.btnUsed.node.active = false;
        this.btnUse.node.active = false;
        this.btnGet.node.active = false;
        this.loadIcon();
        break;
      case ShopState.NOT_USED:
        this.btnBuy.node.active = false;
        this.btnUsed.node.active = false;
        this.btnGet.node.active = false;
        this.btnUse.node.active = true;
        break;
      case ShopState.USED:
        this.btnBuy.node.active = false;
        this.btnUsed.node.active = true;
        this.btnUse.node.active = false;
        this.btnGet.node.active = false;
        break;
      case ShopState.GET:
        this.btnBuy.node.active = false;
        this.btnUsed.node.active = false;
        this.btnUse.node.active = false;
        this.btnGet.node.active = true;
        break;
    }
  }

  buyItem() {
    if (model.user.getItemNum(this._data.currencytype) < this._data.cost) {
      return GlobalObject.tips.show('所需材料不足')
    }
    GlobalObject.confirm.getComponent(Confirm).showBuy(this._data.id, () => {
      apiUtils.buyItem(this._data.id);
      model.commonResult.once('0', () => {
        this._data.state = utils.getSwimState(this._data);
        this.updateState(this._data.state)
      })
    })
  }

  private loadIcon() {
    this.sellIcon1.active = false;
    this.sellIcon2.active = false;
    this[`sellIcon${this._data.currencytype}`].active = true;
    this.txtPrice.string = this._data.cost.toString()
  }

  set data(value: HeadShopData) {
    this._data = value;
    this.txtPrice.string = value.cost.toString();
    this.updateState(value.state)
  }

  onUse() {
    apiUtils.useItem(this._data.id, 1)
    model.commonResult.once('0', () => {
      model.user.activeRole = this._data.id;
      this.node.emit('head-change')
    })
  }

  onGet() {
    const way = this._data.way;
    Statistic.emit(StatEvent.BTN_HEAD_GET, {id: this._data.id})
    cc.director.loadScene('menu', () => {
      const menu = cc.director.getScene().getChildByName("menu").getComponent(Menu);
      menu.openBoard(way)
    })
  }

}